package com.oyjd;

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DownSoft {
	public static String url = "http://apk.hiapk.com";
	public static String url_app_list = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList";
	public static String url_game_list = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList";

	public static String soft_url = "http://static.apk.hiapk.com";

	public static String reg1 = "<span class=\"font14 list_title\"><a href=\"(.*?)\" title=\".*?\">";

	public static String reg_detail_1 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftName\">(.*?)</label>";
	public static String reg_detail_2 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftVersionName\">(.*?)</label>";
	public static String reg_detail_3 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftDeveloper\">(.*?)<";
	public static String reg_detail_4 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftSuitSdk\">([0-9]+(.[0-9]+)?).*?</label>";
	public static String reg_detail_5 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_Description\">(.*?)</label>";
	public static String reg_detail_6 = "<label\\s*id=\"ctl00_AndroidMaster_Content_Apk_SoftUpdateDescription\">(.*?)</label>";
	public static String reg_detail_7 = "<img\\s*width=\"72\"\\s*height=\"72\" src=\"(.*?)\".*?>";
	public static String reg_detail_8 = "<div\\s*class=\"butn_bg butn_3 btnmarg\\s*\">\\s*<a href=\"(.*?)\" class=\"linkbtn d1\".*?>";

	public static String reg_detail_91 = "<div class=\"screenimg\">\\s*<ul>(.*?)</ul>.*?</div>";
	public static String reg_detail_92 = "<img src=\"(.*?)\".*?/>";

	public static String hot_sql = "insert into T_HOT_SOURCE (DOWN_COUNT, SOU_ID, SOU_TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";
	public static String order_sql = "insert into T_SOFT_TYPE_ORDER (ORDER_KEY, SOFT_ID, TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";

//	public static String souPath = "J:/temp/";
//	public static String sqlPath_app = "J:/temp/soft_app.sql";
//	public static String sqlPath_game = "J:/temp/soft_game.sql";
	public static String souPath = "D:/SL/trunk/soft/";
	public static String sqlPath_app = "D:/SL/trunk/script/soft_app.sql";
	public static String sqlPath_game = "D:/SL/trunk/script/soft_game.sql";

	public static boolean isGame = false;// 控制当前是取【游戏】 还是【 应用】
	public static int threadNum = 10;// 开启线程数

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		if (args.length > 0) {
			isGame = args[0].equals("game");
			threadNum = Integer.parseInt(args[1]);
		}

		HttpNet.setRequestProperty("Referer", url);
		System.out.println("***********程序开始运行！***********");
		System.out.println(isGame ? "当前取游戏版面数据" : "当前取应用版面数据");
		final StringBuffer sb = new StringBuffer("use sldb;\r\n\r\n");
		final StringBuffer sb1 = new StringBuffer("\r\n#==============================\r\n");
		final StringBuffer sb_firstType = new StringBuffer("\r\n#==============================\r\n");
		final StringBuffer sb_recomType = new StringBuffer("\r\n#==============================\r\n");
		final StringBuffer sb_gameType = new StringBuffer("\r\n#==============================\r\n");
		final StringBuffer sb_allType = new StringBuffer("\r\n#==============================\r\n");

		HttpNet hn = new HttpNet();
		// 解析软件地址
		final List<String> all = new ArrayList<String>();
		for (int i = 1; i <= 50; i++) {
			System.out.println("------->page:" + i);
			String page = getList(hn, i, isGame ? url_game_list : url_app_list);
			List<String> tmpList = RegexUtil.getMuti(page, reg1);
			for (String s : tmpList) {
				if (!all.contains(s)) {
					all.add(s);
				}
			}
			Thread.sleep(3000);
		}

		System.out.println("===========>共" + all.size() + "个软件");

		new MyTask<String>(threadNum, all, new MyTaskLogic<String>() {

			public void doLogic(int count, int pos, String source) {
				HttpNet hn = new HttpNet();
				String c = null;
				try {
					c = new String(hn.getBytesFromNet((String) source), "utf-8");
				} catch (Exception e) {
					System.out.println("软件明细的url错误======>" + source);
					return;
				}
				// 解析
				String name = RegexUtil.getSingle(c, reg_detail_1);
				String ver = RegexUtil.getSingle(c, reg_detail_2);
				String author = RegexUtil.getSingle(c, reg_detail_3);
				String sdk = RegexUtil.getSingle(c, reg_detail_4);
				String desc = RegexUtil.getSingle(c, reg_detail_5);
				String upDesc = RegexUtil.getSingle(c, reg_detail_6);
				String icon = RegexUtil.getSingle(c, reg_detail_7);
				String sou = RegexUtil.getSingle(c, reg_detail_8);
				String imgs = RegexUtil.getSingle(c, reg_detail_91);
				List<String> imgList = new ArrayList<String>();
				if (null != imgs && !"".equals(imgs)) {
					for (String img : RegexUtil.getMuti(imgs, reg_detail_92)) {
						imgList.add(img);
					}
				}

				// 特殊处理
				name = filter(name);
				author = filter(author);
				desc = filter(desc);
				upDesc = filter(upDesc);

				// 下载
				String iconName = downSou(hn, icon, icon, "sl_icon");
				String souName = downSou(hn, soft_url + sou, soft_url + sou + ".apk", "sl_apk");
				List<String> imgNameList = new ArrayList<String>();
				for (String imgUrl : imgList) {
					String tmpShotName = downSou(hn, imgUrl, imgUrl, "sl_shotcut");
					File imgFile = new File(souPath + tmpShotName);
					if (imgFile.exists()) {
						imgNameList.add(tmpShotName);
					}
				}

				// 合成sql
				String tSql = "";
				int limit = imgNameList.size() > 5 ? 5 : imgNameList.size();
				for (int j = 0; j < limit; j++) {
					tSql += ", '" + imgNameList.get(j) + "'";
				}
				for (int j = 0; j < 5 - imgNameList.size(); j++) {
					tSql += ", ''";
				}

				long souSize = new File(souPath + souName).length();

				String souId = FileOP.getUUID();
				String sql = "insert into T_SOFT (SOFT_ID, APPLY_COMP, AUTHOR, DESCRIPTION, ICON_PATH, NAME, SOU_PATH, SPACE_SIZE, UPDATE_DESC, VER, SCREEN_SHOT1, SCREEN_SHOT2, SCREEN_SHOT3, SCREEN_SHOT4, SCREEN_SHOT5, STATUS, CREATOR, CREATE_TIME, LAST_EDITOR, LAST_UPDATE_TIME) values ";
				sql += "('" + souId + "', '" + sdk + "', '" + author + "', '" + desc + "', '" + iconName + "', '" + name + "', '" + souName + "', " + souSize + ", '" + upDesc + "', '" + ver + "'" + tSql + ", 0, 'admin', now(), 'admin', now());";

				String newHotSql = MessageFormat.format(hot_sql, count - pos, souId, "TYPE_SOFT", FileOP.getUUID());

				String gameTypeSql = MessageFormat.format(order_sql, pos, souId, isGame ? "Soft_Game" : "Soft_App", FileOP.getUUID());
				// 此处交替2,4,6,8 ... 与 1,3,5,7... 保证game 和 app 共同排序
				int dxPos = isGame ? pos * 2 + 1 : pos * 2 + 2;
				String allTypeSql = MessageFormat.format(order_sql, dxPos, souId, "Soft_All", FileOP.getUUID());
				String firstSql = MessageFormat.format(order_sql, dxPos, souId, "Soft_BoutiqueForFirstPage", FileOP.getUUID());
				String recommandSql = MessageFormat.format(order_sql, dxPos, souId, "Soft_Boutique", FileOP.getUUID());
				//
				sb.append(sql).append("\r\n");
				sb1.append(newHotSql).append("\r\n");
				sb_gameType.append(gameTypeSql).append("\r\n");
				sb_allType.append(allTypeSql).append("\r\n");

				if (pos < 10) {// 精品从game 和 app 分别取前10
					sb_firstType.append(firstSql).append("\r\n");
					sb_recomType.append(recommandSql).append("\r\n");
				}

				System.out.println(pos + "----->" + name + "--->完成");
				try {
					Thread.sleep(3000L);
				} catch (InterruptedException e) {					
				}
			}

			public void finishAll(List<String> s) {
				FileOP.writeFile(isGame ? sqlPath_game : sqlPath_app, sb.toString() + sb1.toString() + sb_gameType.toString() + sb_allType.toString() + sb_firstType.toString() + sb_recomType.toString(), "gb2312");
				System.out.println("***********程序运行结束！***********");
			}
		}).start();

	}

	/**
	 * 特殊字符处理
	 * 
	 * @param s
	 * @return
	 */
	private static String filter(String s) {
		s = null == s ? "" : s.replaceAll("&nbsp;", " ").replaceAll("</br>", "\r\n").trim();
		s = null == s || "".equals(s.trim()) ? "" : s.trim().replaceAll("\\r\\n", "\\\\r\\\\n").replaceAll("'", "''");
		return s;
	}

	/**
	 * 下载资源
	 * 
	 * @param hn
	 * @param url
	 * @param oldName
	 * @param pre
	 * @return
	 */
	public static String downSou(HttpNet hn, String url, String oldName, String pre) {
		String newName = FileOP.getNewHashName(oldName, pre);
		File file = new File(souPath + newName);
		if (file.exists() && file.length() == 0) {
			file.delete();
		}
		if (!file.exists()) {
			try {
				hn.downFile(url, null, souPath + newName);
				if (!new File(souPath + newName).exists()) {
					System.out.println("无效链接----->" + url);
				}
			} catch (Exception e) {
				System.out.println("异常链接----->" + url);
			}
		}
		return newName;
	}

	/**
	 * 获取列表
	 * 
	 * @param hn
	 * @param page
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getList(HttpNet hn, int page, String url) throws Exception {
		Map<String, Object> p = new HashMap<String, Object>();
		p.put("curPageIndex", page);
		p.put("sortType", 1);
		p.put("categoryId", 0);
		p.put("totaldownloads", 0);
		p.put("ratings", 0);
		p.put("commentcount", 0);
		p.put("language", 0);
		p.put("platform", 0);
		p.put("currentHash", page + "_1_1_0_0_0_0_0");
		p.put("isInit", false);
		p.put("isPage", true);
		String c = new String(hn.getBytesFromNet(url, p));
		return c;
	}

}
